package com.tedu.dao.impl;

import com.tedu.dao.EmpDao;
import com.tedu.entity.Emp;
import com.tedu.utils.DataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EmpDaomImpl implements EmpDao {
    @Override
    public List<Emp> queryAllEmp() throws SQLException {
        Connection connection = DataSource.getConnection();
        PreparedStatement pstm = connection.prepareStatement("SELECT * FROM emp");
        ResultSet resultSet = pstm.executeQuery();
        ArrayList<Emp> emps = new ArrayList<>();
        while (resultSet.next()){

            Emp emp = new Emp();
            emps.add(
                     emp.setId(resultSet.getInt(1)).
                     setName(resultSet.getString(2))
                    .setSalary(resultSet.getDouble(3))
                    .setBirthday(resultSet.getDate(4))
                    .setAge(resultSet.getInt(5))
                    .setDeptId(resultSet.getInt(6))
            );
        }
        return emps;
    }
}
